//
//  DrawerViewController.m
//  Slice
//
//  Created by lanou on 16/6/13.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "DrawerViewController.h"

@interface DrawerViewController ()<UINavigationControllerDelegate>

@end

@implementation DrawerViewController

- (instancetype)initWithLeftViewController:(UIViewController *)leftViewController MainViewController:(UIViewController *)MainViewController {
    if ([super init]) {
//        添加子视图控制器 如果在一个试图控制器里面使用了另外控制器的View 需要将这个控制器添加为其子视图控制器 防止其被销毁,导致其看不见
        [self addChildViewController:leftViewController];
        [self addChildViewController:MainViewController];
//        左侧控制器添加在最下面
        [self.view addSubview:leftViewController.view];
        [self.view addSubview:MainViewController.view];
        
        self.mainView = MainViewController.view;
        self.leftView = leftViewController.view;
//        初始化平移手势
        self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan1)];
        [self.mainView addGestureRecognizer:self.pan];
    
//        将leftView 的frame 修改到屏幕的左边,来做一个平移效果
        CGRect leftFrame = self.leftView.frame;
        leftFrame.origin.x -= self.view.frame.size.width * kb / 2;
        self.leftView.frame = leftFrame;
    }
    return self;
}

//实现手势的方法
- (void)pan1 {
//    获取手势的位置
    CGPoint userP =  [self.pan locationInView:self.mainView];
    
    CGPoint userPoint =  [self.pan translationInView:self.mainView];
    
    if (userP.x > self.mainView.frame.size.width / 2 ) {
        return;
                
    }
    
        //得打原始的frame
        CGRect mainFrame = self.mainView.frame;
        mainFrame.origin.x += userPoint.x;
        
        //    判断是否是在可滑动范围
        if (mainFrame.origin.x >=  0 && mainFrame.origin.x <= self.view.frame.size.width * kb) {
            
            //        在移动的范围内移动
            
            CGFloat a1 = self.mainView.frame.origin.x / (self.view.frame.size.width * kb ) ;
            
            self.leftView.transform = CGAffineTransformMakeScale(2 - a1, 2 - a1);
            //        mainView 的frame 跟着改变
            self.mainView.frame = mainFrame;
            
            //另外一种方法可以
            CGRect leftFrame = self.leftView.frame;
            
            leftFrame.origin.x = mainFrame.origin.x / 2 - self.view.frame.size.width * kb / 2;
            
            self.leftView.frame = leftFrame;
           
            
        }
        
        if (self.pan.state == UIGestureRecognizerStateEnded) {
            if (self.mainView.frame.origin.x > self.view.frame.size.width * kb / 2) {
                [self open];
                
            } else {
                
                [self close];
                
            }
            

    }
//    将本次已经移动的点设置为0
    [self.pan  setTranslation:CGPointZero inView:self.mainView];
  
}

//打开抽屉
- (void)open {
   [UIView animateWithDuration:0.3 animations:^{
       
       //    修改leftView的缩放
       self.leftView.transform = CGAffineTransformMakeScale(1, 1);
//        获取原始的frame
       CGRect mainFrame = self.mainView.frame;
//       修改x 值 使其向右边移动
       mainFrame.origin.x = self.view.frame.size.width * kb;
       
       self.mainView.frame = mainFrame;
       
//       修改leftView 的frame
       self.leftView.frame = self.view.frame;
       
   } completion:^(BOOL finished) {
       self.isOpen = YES;
   }];
}
//关闭抽屉
-(void) close{

   [UIView animateWithDuration:0.3 animations:^{
       //    修改leftView的缩放
       self.leftView.transform = CGAffineTransformMakeScale(2, 2);
       self.mainView.frame = self.view.frame;
//        将leftView 的frame 修改到屏幕的左边,来做一个平移效果
       CGRect leftFrame = self.leftView.frame;
       leftFrame.origin.x = -self.view.frame.size.width *kb / 2;
       self.leftView.frame = leftFrame;
   } completion:^(BOOL finished) {
       self.isOpen = NO;
   }];
    
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    }

//切换主视图控制器
- (void)setMainViewController:(UIViewController *)vc {
    
    if ([vc isMemberOfClass:[UINavigationController class]]) {
        UINavigationController *navc = (UINavigationController *)vc;
        navc.delegate = self;
    }
    
    
//    判断是否存在已经添加的这个控制器
    if (![self.childViewControllers containsObject:vc]) {
        [self addChildViewController:vc];
    }
    
    if (vc.view == self.mainView) {
        return;
    }
    
//    CGRect rect = self.mainView.frame;
//    
//    rect.origin.y -= kHeight;
    
    
    vc.view.frame = CGRectMake(self.mainView.frame.origin.x,kHeight , kWidth, kHeight);
//     vc.view.frame = CGRectMake(0,0 , kWidth, kHeight);
    
    CGRect rect = self.mainView.frame;
    
    rect.origin.y -= kHeight;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.leftView.userInteractionEnabled = NO;
        
        //    用当前主视图的frame作为新的视图的frame
        
        vc.view.frame = self.mainView.frame;
        
        self.mainView.frame = rect;
        
        //    添加到视图上面
        [self.view addSubview:vc.view];
    } completion:^(BOOL finished) {
            //    移除视图及相应的手势]
            [self.mainView removeFromSuperview];
            [self.mainView removeGestureRecognizer:self.pan];
            //    让mainview 的视图指向新的mainView
            self.mainView = vc.view;
            //    给新的添加手势
            [self.mainView addGestureRecognizer: self.pan];
        
         self.leftView.userInteractionEnabled = YES;
        
        [self close];
    }];

}


- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 如果将要显示的VC 就是rootVC 把手势添加回来 如果不是移除掉
    if (navigationController.childViewControllers[0] == viewController) {
        [self.mainView addGestureRecognizer:self.pan];
    }else {
        [self.mainView removeGestureRecognizer:self.pan];
    }
}













/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
